<template>
  <div>
    <!-- 返利池查询 -->
    <TopFilter ref="top" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="wkcRebateInvoiceQuery"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
    >
      <template slot="controls">
        <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
      </template>
    </FilterTable>
   <Drawer :visible.sync="isDetail" :title="'返利红票申请明细'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="detailData" @close="detailclose" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, excelList } from '@/api/omFinanceP/RebateManage/RebateInvoiceQuery';
import { exportExcelFile } from '@/utils';
import detailModel from './detailModel';
export default {
  name: 'rebateInvoiceQuery',
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
      },
      formData: {},
      detailData: {},
      addData: {},
      visible: false,
      isClick: false,
      isDetail: false,
      handleType: '',
      modelName: '',
    };
  },
  components: { detailModel },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    addHandle() {
      //导出
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // this.$refs.top.SUBMIT_FORM();
          // this.exportLoading = true;
          excelList(this.fetchParams)
            .then((response) => {
              exportExcelFile(response.data, '返利红票查询');
              // this.exportLoading = false;
            })
            .catch((err) => {
              //console.log(err);
            });
        })
        .catch(() => {
          // this.exportLoading = false;
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    detailclose(val) {
      this.isDetail = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    async editHandle(data, type) {
      if (type === 2) {
        this.detailData = data;
        this.isDetail = true;
      }
    },
    formatNumber(value) {
      value += '';
      if (value.slice(-1) === '.') return value;
      value = value.replace(/,\s+/g, '');
      const list = value.split('.');
      const prefix = list[0].charAt(0) === '-' ? '-' : '';
      let num = prefix ? list[0].slice(1) : list[0];
      let result = '';
      while (num.length > 3) {
        result = `, ${num.slice(-3)}${result}`;
        num = num.slice(0, num.length - 3);
      }
      if (num) {
        result = num + result;
      }
      return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '返利红票申请单号',
          fieldName: 'applyNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2157'),
        },
        {
          type: 'RANGE_DATE',
          label: '申请时间',
          fieldName: 'beginApplyDate|endApplyDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
        },
      ];
    },
     createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  明细
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2157'),
          render: (props) => {
            return <span>{this.createDictText(props.row.applyStatus, '2157')}</span>;
          },
        },
        {
          title: '返利红票申请单号',
          width: 160,
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '所属区域',
          dataIndex: 'areaName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          width: 120,
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '申请人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '申请时间',
          dataIndex: 'applyDate',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true,
          filter: true,
          filterType: 'input',
        },

        {
          title: '申请金额',
          dataIndex: 'totalApplyAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '发票号',
          dataIndex: 'sapInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '开票时间',
          dataIndex: 'sapInvoiceDate',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true,
          filter: true,
          filterType: 'input',
        },

        {
          title: '开票金额',
          dataIndex: 'sapInvoiceAmount',
          sorter: true,
          filter: true,
          precision: 2,
          numberFormat: true,
          align: 'right',
          filterType: 'input',
        },
        {
          title: '申请原因',
          dataIndex: 'applyReason',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '审核意见',
          dataIndex: 'checkDesc',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '作废原因',
          dataIndex: 'cancelReason',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ];
    },
  },
};
</script>
